package com.lamda.zhekou.controller;

import com.lamda.zhekou.manager.ServerHostManager;
import com.lamda.zhekou.model.Person;
import com.lamda.zhekou.model.VpsBuyRecord;
import com.lamda.zhekou.utils.excel.ExcelData;
import com.lamda.zhekou.utils.excel.ExportExcel;
import com.lamda.zhekou.vo.IncomeVo;
import com.lamda.zhekou.vo.IncomeTotalVo;
import com.lamda.zhekou.vo.VPSTotalVo;
import com.lamda.zhekou.vo.VpsRecordVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

/**
 * Created by MJiao on 15/8/28.
 */
@Controller
public class IncomeController {

    @Autowired
    private ServerHostManager serverHostManager;

    @RequestMapping(value = "/income")
    public String income(HttpServletRequest httpServletRequest, Model model) {
        List<VpsBuyRecord> vpsBuyRecordList = this.serverHostManager.getAllVpsBuyRecordList();
        List<IncomeTotalVo> incomeTotalVoList = this.serverHostManager.getIncomeTotalList();
        model.addAttribute("vpsBuyRecordList", vpsBuyRecordList);
        model.addAttribute("incomeTotalVoList", incomeTotalVoList);
        return "income";
    }

    @RequestMapping(value = "/downloadReport")
    @ResponseBody
    public String testDownload(HttpServletRequest httpServletRequest, HttpServletResponse response,
                               @RequestParam(value = "startTime", required = true) String startTime,
                               @RequestParam(value = "hostId", required = true) Long hostId,
                               @RequestParam(value = "endTime", required = true) String endTime) {

        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy年MM月dd日");
        try {

            Timestamp startTimestamp = new Timestamp(simpleDateFormat.parse(startTime).getTime());
            Timestamp endTimestamp = new Timestamp(simpleDateFormat.parse(endTime).getTime());


            Calendar endCalendar = Calendar.getInstance();
            endCalendar.setTimeInMillis(endTimestamp.getTime());

            Calendar calendar = Calendar.getInstance();
            calendar.set(Calendar.YEAR, endCalendar.get(Calendar.YEAR));
            calendar.set(Calendar.MONDAY, endCalendar.get(Calendar.MONDAY));
            calendar.set(Calendar.DAY_OF_MONTH, endCalendar.get(Calendar.DAY_OF_MONTH));
            calendar.set(Calendar.HOUR_OF_DAY, 23);
            calendar.set(Calendar.MINUTE, 59);
            calendar.set(Calendar.SECOND, 59);

            endTimestamp = new Timestamp(calendar.getTimeInMillis());

            List<VpsBuyRecord> vpsBuyRecords = this.serverHostManager.findVpsBuyRecords(hostId, startTimestamp, endTimestamp);
            List<IncomeTotalVo> incomeTotalVoList = this.serverHostManager.getIncomeTotalList(hostId, startTimestamp, endTimestamp);


            List<String> titles1 = new ArrayList<>();
            titles1.add("购买人");
            titles1.add("VPS");
            titles1.add("购买金额");
            titles1.add("购买时间");
            titles1.add("操作代码");
            titles1.add("操作人");


            List list1 = new ArrayList<>();
            for (VpsBuyRecord vpsBuyRecord : vpsBuyRecords) {

                VpsRecordVo vpsRecordVo = new VpsRecordVo();

                vpsRecordVo.setBuyTime(vpsBuyRecord.getCreateTime().toString());
                vpsRecordVo.setCustomer(vpsBuyRecord.getCustomer().getNickname());
                vpsRecordVo.setRmb(vpsBuyRecord.getRmb() + "");
                vpsRecordVo.setSeller(vpsBuyRecord.getSeller().getNickname() + "");
                vpsRecordVo.setOperateType(vpsBuyRecord.getOperatorCode());

                list1.add(vpsRecordVo);
            }
            ExcelData excelData1 = new ExcelData();
            excelData1.setSheetName("详细");
            excelData1.setTittles(titles1);
            excelData1.setDataList(list1);


            List list2 = new ArrayList<>();
            List<String> titles2 = new ArrayList<>();
            titles2.add("姓名");
            titles2.add("金额");

            for (IncomeTotalVo incomeTotalVo : incomeTotalVoList) {
                VPSTotalVo vpsTotalVo = new VPSTotalVo();
                vpsTotalVo.setRmb(incomeTotalVo.getRmb()+"");
                vpsTotalVo.setSeller(incomeTotalVo.getNickname());
                list2.add(vpsTotalVo);
            }
            ExcelData excelData2 = new ExcelData();
            excelData2.setSheetName("总计");
            excelData2.setTittles(titles2);
            excelData2.setDataList(list2);

            ExportExcel.exportExcel(response, "流水统计.xls", excelData1, excelData2);

        } catch (ParseException e) {
            e.printStackTrace();

        }

        return "OK";
    }

    @RequestMapping(value = "/findIncome")
    @ResponseBody
    public IncomeVo doRenewVps(@RequestParam(value = "startTime", required = true) String startTime,
                               @RequestParam(value = "hostId", required = true) Long hostId,
                               @RequestParam(value = "endTime", required = true) String endTime) {
        IncomeVo incomeVo = new IncomeVo();

        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy年MM月dd日");
        try {

            Timestamp startTimestamp = new Timestamp(simpleDateFormat.parse(startTime).getTime());
            Timestamp endTimestamp = new Timestamp(simpleDateFormat.parse(endTime).getTime());


            Calendar endCalendar = Calendar.getInstance();
            endCalendar.setTimeInMillis(endTimestamp.getTime());

            Calendar calendar = Calendar.getInstance();
            calendar.set(Calendar.YEAR, endCalendar.get(Calendar.YEAR));
            calendar.set(Calendar.MONDAY, endCalendar.get(Calendar.MONDAY));
            calendar.set(Calendar.DAY_OF_MONTH, endCalendar.get(Calendar.DAY_OF_MONTH));
            calendar.set(Calendar.HOUR_OF_DAY, 23);
            calendar.set(Calendar.MINUTE, 59);
            calendar.set(Calendar.SECOND, 59);

            endTimestamp = new Timestamp(calendar.getTimeInMillis());

            //用户的购买记录
            List<VpsBuyRecord> vpsBuyRecords = this.serverHostManager.findVpsBuyRecords(hostId, startTimestamp, endTimestamp);

            List<IncomeTotalVo> incomeTotalVoList = this.serverHostManager.getIncomeTotalList(hostId, startTimestamp, endTimestamp);

            incomeVo.setVpsBuyRecords(vpsBuyRecords);
            incomeVo.setIncomeTotalVoList(incomeTotalVoList);

        } catch (ParseException e) {
            e.printStackTrace();

        }
        return incomeVo;
    }

}
